home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / FANGS__ / OFFSETLI.C < prev    next >
Text File  |  1990-05-29  |  5KB  |  206 lines

  1. /* ⌐ 1990 Patrick Doane. */
  2.  
  3. #include <OffsetList.h>
  4. #include <oops.h>
  5. #include <Utilities.h>
  6.  
  7. extern GrafPtr    scratchPort;
  8.  
  9. void    OffsetList::IOffsetList(WindowPtr theWindow,Rect *theBounds,
  10.     Boolean hasVert,short theRows,short theRowWidth,ShortFunc theCompareProc,
  11.     VoidFunc theDoubleProc,VoidFunc theChangeFunc)
  12. {
  13.     Rect    theRect;
  14.     
  15.     SelectList::ISelectList(theWindow,theBounds,hasVert,theRows,
  16.         theRowWidth,theCompareProc,theDoubleProc);
  17.     
  18.     theRect.left = theBounds->left - 35;
  19.     theRect.right = theBounds->left - 1;
  20.     theRect.top = theBounds->top - 1;
  21.     theRect.bottom = theRect.top + 16;
  22.     itsOffsetNum = new(OffsetNum);
  23.     if (theChangeFunc)
  24.             itsOffsetNum->IOffsetNum(theWindow,&theRect,0x8000,0x8000,
  25.         0x8000,0,theChangeFunc,0);
  26.     else
  27.             itsOffsetNum->IOffsetNum(theWindow,&theRect,0x8000,0x8000,
  28.         0x8000,0,ChangeList,0);
  29.     itsOffsetNum->refCon = (long)this;
  30. }
  31.  
  32. void    OffsetList::Dispose(void)
  33. {
  34.     inherited::Dispose();
  35. }
  36.  
  37. void    OffsetList::SetSelection(short theSelection)
  38. {
  39.     InvertRow(currentSelection);
  40.     currentSelection = theSelection;
  41.     InvertRow(currentSelection);
  42.     MoveArrow();
  43. }
  44.  
  45. void    OffsetList::Scroll(short hDelta,short vDelta,Boolean redraw)
  46. {
  47.     short        hPixels;
  48.     short        vPixels;
  49.     Rect        theRect;
  50.     RgnHandle    invalRgn;
  51.     
  52.     invalRgn = NewRgn();
  53.     hPixels = hDelta * hScale;
  54.     vPixels = vDelta * vScale;
  55.     
  56.     if (redraw)
  57.     {    theRect = bounds;
  58.         ScrollRect(&theRect,-hPixels,-vPixels,invalRgn);
  59.         InvalRgn(invalRgn);
  60.     }
  61.  
  62.     position.h += hDelta;
  63.     position.v += vDelta;
  64.  
  65.     MoveArrow();
  66.     if (redraw)
  67.     {    BeginUpdate(thePort);
  68.         Update();
  69.         EndUpdate(thePort);
  70.     }
  71. }
  72.  
  73. void    OffsetList::DrawRow(short rowNum)
  74. {
  75.     long            rowSize;
  76.     Ptr                rowPtr;
  77.     register Rect    theBounds;
  78.     register OffsetListRec    theRecord;
  79.     Str255            theString;
  80.     short            vOffset;
  81.     
  82.     if (ValidRow(rowNum))
  83.     {    theBounds = bounds;
  84.         vOffset = bounds.top + rowHeight * (rowNum - position.v) - vIndent;
  85.         MoveTo(bounds.left + hIndent,vOffset);
  86.         rowPtr = GetRowOffset(rowNum);
  87.         
  88.         theRecord = *(OffsetListRec *)rowPtr;
  89.         DrawString(theRecord.title);
  90.         NumToString(theRecord.currentValue,theString);
  91.         MoveTo(bounds.left + hIndent+120,vOffset);
  92.         DrawString(theString);
  93.     }
  94. }
  95.  
  96. void    OffsetList::DoClick(EventRecord *theEvent)
  97. {
  98.     Rect    theBounds,theRect,arrowBounds;
  99.     short    lastItem;
  100.     Point    mouseLoc;
  101.     Boolean    newState;
  102.     
  103.     theBounds = bounds;
  104.     lastItem = currentSelection;
  105.     currentSelection = (theEvent->where.v - theBounds.top +
  106.         (position.v * rowHeight)) / rowHeight + 1;
  107.     if (ValidRow(currentSelection))
  108.     {    if (currentSelection == clickRow && (theEvent->when - clickTime)
  109.                 < GetDblTime())
  110.             (*doubleProc)(this);
  111.         else
  112.         {    InvertRow(lastItem);
  113.             InvertRow(currentSelection);
  114.             lastItem = currentSelection;
  115.             MoveArrow();
  116.         }
  117.     }
  118.     else
  119.     {    InvertRow(currentSelection);
  120.         currentSelection = 0;
  121.     }
  122.     clickTime = theEvent->when;
  123.     clickRow = currentSelection;
  124.     while (TrackMouse(&mouseLoc,&theBounds,&newState,0))
  125.     {    currentSelection = (mouseLoc.v - theBounds.top +
  126.             (position.v * rowHeight)) / rowHeight + 1;
  127.         if (ValidRow(currentSelection))
  128.         {    if (currentSelection != lastItem)
  129.             {    InvertRow(lastItem);
  130.                 InvertRow(currentSelection);
  131.                 lastItem = currentSelection;
  132.                 MoveArrow();
  133.             }
  134.         }
  135.         else
  136.         {    InvertRow(lastItem);
  137.             currentSelection = 0;
  138.             lastItem = currentSelection;
  139.             itsOffsetNum->Hide();
  140.             arrowBounds = itsOffsetNum->bounds;
  141.             EraseRect(&arrowBounds);
  142.         }
  143.     }
  144.     clickTime = theEvent->when;
  145.     clickRow = currentSelection;
  146. }
  147.  
  148. void    OffsetList::MoveArrow(void)
  149. {
  150.     Rect    arrowBounds;
  151.     Rect    theRect;
  152.     
  153.     if (currentSelection >= position.v + 1 && currentSelection <= position.v + visRows)
  154.     {    arrowBounds = itsOffsetNum->bounds;
  155.         theRect.left = bounds.left - 35;
  156.         theRect.right = bounds.left - 1;
  157.         theRect.top = bounds.top - 1 +
  158.             (currentSelection - 1 - position.v) * rowHeight;
  159.         theRect.bottom = theRect.top + 16;
  160.         itsOffsetNum->SetBounds(&theRect);
  161.         EraseRect(&arrowBounds);
  162.         itsOffsetNum->Show();
  163.         itsOffsetNum->Update();
  164.     }
  165.     else
  166.     {    itsOffsetNum->Hide();
  167.         arrowBounds = itsOffsetNum->arrowBounds;
  168.         EraseRect(&arrowBounds);
  169.     }
  170. }
  171.  
  172. void    ChangeList(register OffsetNum *theOffsetNum,short theParam)
  173. {
  174.     register OffsetList        *theList;
  175.     register OffsetListRec    *theRecord;
  176.     short                    currentRow;
  177.     GrafPtr                    savePort;
  178.     Rect                    theRect;
  179.     
  180.     theList = (OffsetList *)theOffsetNum->refCon;
  181.     currentRow = theList->currentSelection;
  182.     theRecord = (OffsetListRec *)theList->GetRowOffset(currentRow);
  183.     
  184.     switch (theParam)
  185.     {    case DoIncrease:        
  186.             theRecord->currentValue += theRecord->stepValue;
  187.             theRecord->offset += theRecord->stepValue;
  188.             break;
  189.         case DoDecrease:
  190.             theRecord->currentValue -= theRecord->stepValue;
  191.             theRecord->offset -= theRecord->stepValue;
  192.             break;
  193.     }
  194.     GetPort(&savePort);
  195.     SetPort(scratchPort);
  196.     theRect = theList->bounds;
  197.     theRect.top = theList->rowHeight * (currentRow - 1 -
  198.         theList->position.v) + theList->bounds.top;
  199.     theRect.bottom = theRect.top + theList->rowHeight;
  200.     EraseRect(&theRect);
  201.     theList->DrawRow(currentRow);
  202.     theList->InvertRow(currentRow);
  203.     SetPort(savePort);
  204.     CopyBits(&scratchPort->portBits,&thePort->portBits,&theRect,&theRect,
  205.         srcCopy,0L);
  206. }